﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Task.DownloadImage.Utilities;

using System.IO;

namespace Task.DownloadImage.UserControls
{
    /// <summary>
    /// WaitingPanel.xaml 的交互逻辑
    /// </summary>
    public partial class WaitingPanel : UserControl
    {
        private Storyboard story;

        public string ImgSrcAddr
        {
            get { return (string)GetValue(ImgSrcAddrProperty); }
            set { SetValue(ImgSrcAddrProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ImgSrcAddr.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImgSrcAddrProperty =
            DependencyProperty.Register("ImgSrcAddr", typeof(string), typeof(WaitingPanel), new PropertyMetadata(string.Empty, OnSourceChanged));

        public ImageSource Source
        {
            get { return (ImageSource)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Source.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source", typeof(ImageSource), typeof(WaitingPanel), new PropertyMetadata(new BitmapImage(new Uri("pack://application:,,,/Task.DownloadImage;component/Images/WaitingCircle.png", UriKind.Absolute))));

        /// <summary>
        /// 当源地址更新后，加载此资源
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (string.IsNullOrEmpty((string)e.NewValue)) return;
            string tempDir = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"Temp");
            if (!System.IO.Directory.Exists(tempDir)) Directory.CreateDirectory(tempDir);
            string tempPath = System.IO.Path.Combine(tempDir, $"{Guid.NewGuid().ToString()}.jpg");
            var control = (WaitingPanel)d;
            var resultImg = DoownloadHelper.WebDownloadAsync((string)e.NewValue, tempPath,null);
            control.Source = BitmapToBitmapImage(resultImg);
        }

        public WaitingPanel()
        {
            InitializeComponent();
            this.story = (base.Resources["waiting"] as Storyboard);
            this.Initialized += WaitingPanel_Initialized;
        }

        private void WaitingPanel_Initialized(object sender, EventArgs e)
        {
            //this.Source = new BitmapImage(new Uri("/Task.DownloadImage;component/Images/WaitingCircle.png"));
        }

        private void Image_Loaded_1(object sender, RoutedEventArgs e)
        {
            //this.story.Begin(this.imagePanel, true);
        }

        public void Stop()
        {
            base.Dispatcher.BeginInvoke(new Action(() =>
            {
                this.story.Pause(this.imagePanel);
                base.Visibility = System.Windows.Visibility.Collapsed;
            }));
        }

        public static BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap)
        {
            MemoryStream ms = new MemoryStream();
            bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            BitmapImage bit3 = new BitmapImage();
            bit3.BeginInit();
            bit3.StreamSource = ms;
            bit3.EndInit();
            return bit3;
        }
    }
}
